<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\ArchiveModel;
use app\index\model\BondJiaoModel;
use app\index\model\BondShouModel;
use app\index\model\CertCompanyModel;
use app\index\model\CertModel;
use app\index\model\DaiKouQiListModel;
use app\index\model\DeviceJinListModel;
use app\index\model\DeviceTuiModel;
use app\index\model\DeviceYunModel;
use app\index\model\FlowPromerModel;
use app\index\model\FlowShareModel;
use app\index\model\FlowTaskShenModel;
use app\index\model\LabourXiangListModel;
use app\index\model\PersonModel;
use app\index\model\CertUseListModel;
use app\index\model\CertUseModel;
use app\index\model\ContractBuyListModel;
use app\index\model\ContractBuyModel;
use app\index\model\ContractCaiJieModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractShiModel;
use app\index\model\DangJieListModel;
use app\index\model\DangModel;
use app\index\model\DealcomListModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginGodanListModel;
use app\index\model\EnginGodanModel;
use app\index\model\EnginJianModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginXunListModel;
use app\index\model\EnginXunModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\FlowNodeModel;
use app\index\model\FlowShenlogModel;
use app\index\model\FlowTalkModel;
use app\index\model\FlowTaskModel;
use app\index\model\FlowUsedModel;
use app\index\model\IncomeHuanModel;
use app\index\model\LabourGongModel;
use app\index\model\LabourXiangModel;
use app\index\model\LabourYongModel;
use app\index\model\OptionFeiModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\ReadsModel;
use app\index\model\SealModel;
use app\index\model\SpeedMonthListModel;
use app\index\model\SpeedMonthModel;
use app\index\model\SpeedZongListModel;
use app\index\model\SpeedZongModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\TodoModel;
use app\index\model\UserGangModel;
use app\index\model\UserModel;
use app\index\model\WuCaiListModel;
use app\index\model\WuCaiModel;
use app\index\model\WuChuModel;
use app\index\model\WuLingListModel;
use app\index\model\WuRuListModel;
use app\index\model\ZhiJieModel;
use app\index\model\ZhiJintuiListModel;
use app\index\service\CertService;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\LabourService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PiaoService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Opt extends Common
{

    public function get_readsandlog(Request $request){
        $param = $request->post();
        if(!isset($param['table']) || empty($param['table'])){
            return outputJson(2000, lang('code_2000'), []);
        }
        if(!isset($param['mid']) || empty($param['mid'])){
            return outputJson(2000, lang('code_2000'), []);
        }

        $shuju = Db::name($param['table'])->where(['id'=>$param['mid']])->find();
        if(!$shuju){
            return outputJson(2000, lang('code_2000'), []);
        }

        $dataDelStr = '';
        if(isset($shuju['deleted_user']) && $shuju['deleted_user'] > 0){
            $delUser = UserModel::where(['id'=>$shuju['deleted_user']])->value('realname');
            $delTime = '';
            if($shuju['deleted_at'] && $shuju['deleted_at'] != '0000-00-00 00:00:00'){
                $delTime = date("Y年m月d日 H时i分", strtotime($shuju['deleted_at']));
            }
            $dataDelStr = "当前数据已被【".$delUser." ".$delTime."】删除";
        }



        try {
            TodoModel::where(['uid'=>$this->userinfo['id'],'table'=>$param['table'],'mid'=>$param['mid'],'status'=>0])->update([
                'status'=>1,'readdt'=>date("Y-m-d H:i:s")
            ]);

            $flowlog = FlowLogModel::where(['table'=>$param['table'],'mid'=>$param['mid']])->select()->toArray();
            foreach ($flowlog as &$item){
                $user = UserModel::field('realname,avatar')->where(['id'=>$item['created_user']])->find();
                $item['created_user'] = $user['realname'];
                $item['avatar'] = $user['avatar'];
            }

            $readsModel = new ReadsModel();
            $info = $readsModel->where(['table'=>$param['table'],'mid'=>$param['mid'],'optid'=>$this->userinfo['id']])->find();
            if($info){
                $info->optdt = date('Y-m-d H:i:s');
                $info->stotal = $info['stotal'] + 1;
                $info->save();
            }else{
                $readsModel->add_reads($param['table'], $param['mid'], $this->userinfo['id'],$this->userinfo['comid']);
            }
            $reads = $readsModel->alias('r')->field('r.stotal,r.optdt,u.realname,u.avatar')
                ->join('user u','u.id=r.optid')
                ->where(['table'=>$param['table'],'mid' => $param['mid']])->select()->toArray();
        } catch (\Exception $e) {
            return outputJson(2000, lang('code_2000'), []);
        }
        return outputJson(2000, lang('code_2000'), [
            'dataDelStr' => $dataDelStr,
            'flowlog' => $flowlog,
            'reads' => $reads,
        ]);
    }
    public function get_flowtalk(Request $request){
        $param = $request->post();
        $data = FlowTalkModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$param['id']])->select()->toArray();
        foreach ($data as &$item){
            $item['domer_name'] = UserModel::get_realname_by_ids($item['domer']);

            $uidinfo = UserModel::field('realname,avatar')->where(['id'=>$item['uid']])->find();
            $item['uid_name'] = $uidinfo['realname'];
            $item['uid_avatar'] = $uidinfo['avatar'];

            $item['filesarr'] = FileModel::field('id,table,mid,name,ext,size,encpath,fileplain,created_user,created_at')
                ->where('id','in',$item['files'])->select()->toArray();
        }
        return outputJson(2000, lang('code_2000'), $data);
    }
    public function get_userpart(Request $request){
        $keyword = $request->param('keyword', '', 'string');
        $type = $request->param('type', 'user', 'string');
        $dept = $request->param('dept', '', 'string');

        $where = [];
        if($dept == 'chan'){
            $where[] = ['type', '=', 2];
        }
        if($type == 'dept'){
            $where[] = ['name','like','%'.$keyword.'%'];
        }

        $whereUser = [];
        if($type == 'user'){
            $whereUser[] = ['realname','like','%'.$keyword.'%'];
        }
        $data = DeptModel::where(['status'=>1,'deleted_user'=>0])->where($where)->field('id,pid,name as title,sort')->select()->toArray();
        foreach ($data as &$item){
            $item['basicData'] = ['id'=>$item['id'],'name'=>$item['title'],'part'=>'dept'];
            $item['children'] = [];

            if($type == 'user' || $type == 'part'){
                $users = UserModel::where(['deleted_user'=>0,'status'=>1,'deptid'=>$item['id']])->where($whereUser)
                    ->field('id,realname as title')->select()->toArray();
                foreach ($users as &$user){
                    $user['sort'] = 0;
                    $user['basicData'] = ['id'=>$user['id'],'name'=>$user['title'],'part'=>'user'];
                    $user['name'] = $user['title'];
                    $user['title'] = '&nbsp;&nbsp;<i class="layui-icon layui-icon-username"></i>&nbsp;&nbsp;'.$user['title'];
                }
                $item['children'] = $users;
            }

            if(count($item['children']) > 0){
                $item['title'] = '<i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;'.$item['title'];
            }else{
                $item['title'] = '&nbsp;&nbsp;<i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;'.$item['title'];
            }

        }



        $data = array2tree($data, 'id', 'pid','children','sort');
        return json_encode([
            'status'=>['code'=>200,'message'=>"操作成功"],
            'data'=>$data,
            'code'=>2000,
            'msg'=>'请求成功',
        ]);
    }

    // 部门人员
    public function get_dept_user(){
        $input = input('post.');
        $where = [];
        if(isset($input['keyword']) && !empty($input['keyword'])){
            $where[] = ['realname','like','%'.$input['keyword'].'%'];
        }

        $data = DeptModel::where(['status'=>1,'deleted_user'=>0])->field('id,pid,name as title')->select()->toArray();
        foreach ($data as &$item){
            $rawWhere = Db::raw("find_in_set(".$item['id'].",deptid)");
            $users = UserModel::where($rawWhere)->where(['deleted_user'=>0,'status'=>1])->where($where)->field('id,realname as title')->select()->toArray();
            foreach ($users as &$user){
                $user['name'] = $user['title'];
                $user['title'] = '&nbsp;&nbsp;<i class="layui-icon layui-icon-username"></i>&nbsp;&nbsp;'.$user['title'];
            }
            $item['children'] = $users;

            if(count($item['children']) > 0){
                if(isset($input['keyword']) && !empty($input['keyword'])){
                    $item['spread'] = true;
                }
                $item['title'] = '<i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;'.$item['title'];
            }else{
                $item['title'] = '&nbsp;&nbsp;<i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;'.$item['title'];
            }
        }

        $data = array2tree($data);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_dept(){
        $input = input('post.');
        $where = [];
        if(isset($input['keyword']) && !empty($input['keyword'])){
            $where[] = ['name','like','%'.$input['keyword'].'%'];
        }
        $data = DeptModel::where(['status'=>1,'deleted_user'=>0])->where($where)->field('id,pid,name')->select()->toArray();
        foreach ($data as &$item){
            $item['title'] = $item['name'];
//            $item['title'] = '<i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;'.$item['name'];
        }
        $data = array2tree($data);
        return outputJson(2000,lang('code_2000'),$data);

    }
    public function get_flow_used(Request $request){
        $data['used'] = FlowUsedModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id']])->select()->toArray();

        if($this->userinfo['qian'] && $this->userinfo['qian_id']){
            $qianinfo = ['qian'=>$this->userinfo['qian'],'qian_id'=>$this->userinfo['qian_id']];
        }else{
            $qianinfo = FlowShenlogModel::where(['comid'=>$this->userinfo['comid'],'created_user'=>$this->userinfo['id']])
                ->where('qian_id','gt',0)->order("id DESC")->find();
        }
        $data['qianinfo'] = $qianinfo;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_flow_used(Request $request){
        $explain = $request->param('explain', '', 'string');

        if(empty($explain)){
            return outputJson(4000,lang('code_4000'));
        }

        FlowUsedModel::insertGetId([
            'comid' => $this->userinfo['comid'],
            'uid' => $this->userinfo['id'],
            'explain' => $explain,
        ]);

        $data = FlowUsedModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id']])->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_flow_used(Request $request){
        $id = $request->param('id', 0, 'int');
        FlowUsedModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id'],'id'=>$id])->delete();
        $data = FlowUsedModel::where(['comid'=>$this->userinfo['comid'],'uid'=>$this->userinfo['id']])->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_flow_tui_info(Request $request){
        $param = $request->post();
        $bill = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$param['id']])->find();
        if(!$bill){
            return outputJson(4000, "无法退回");
        }
        if($bill['isturn'] != 1){
            return outputJson(4000, "无法退回");
        }
        if($bill['status'] != 0){
            return outputJson(4000, "无法退回");
        }

        $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();
        if(!$minfo){
            return outputJson(4000, "无法退回");
        }

        $myTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'state'=>1])
            ->where(['uid'=>$this->userinfo['id'],'deleted_user'=>0])->find();
        if(!$myTask){
            return outputJson(4000, "无法退回");
        }

        $node = FlowNodeModel::where(['id'=>$myTask['nodeid']])->find();
        if(!$node){
            return outputJson(4000, "无法退回");
        }

        $listJie = FlowNodeModel::field('id,name')->where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])
            ->where('id','lt', $node['id'])->order('id DESC')->select()->toArray();

        $listRen = [];
        foreach ($listJie as $k=>$v){
            $sontask = FlowTaskModel::field('id,inituid')->where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'nodeid'=>$v['id'],'pid'=>0,'state'=>2])
                ->order('id DESC')->select()->toArray();
            if($sontask){
                foreach ($sontask as $kk=>$vv){
                    $inituid_name = UserModel::get_realname_by_pk($vv['inituid']);
                    $listRen[] = ['id'=>$vv['id'], 'name'=>$v['name'].'：'.$inituid_name];
                }
            }
        }

        $listJie[] = ['id'=>0, 'name'=>'申请人发起申请'];

        if($node['type'] == 2){
            $listJie = [['id'=>0, 'name'=>'申请人发起申请']];
        }

        $data['listJie'] = $listJie;
        $data['listRen'] = $listRen;
        $data['nodetype'] = $node['type'];
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_flowrelas(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bill.deleted_user','=', 0];
        $where[] = ['bill.comid','=',$this->userinfo['comid']];
        $where[] = ['bill.isturn','=', 1];
        $where[] = ['bill.status','=', 1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|bill.summary|e.name|bill.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['summary']) && !empty($param['summary'])){
            $where[] = ['bill.summary','like', '%'.$param['summary'].'%'];
        }
        if(isset($param['enginame']) && !empty($param['enginame'])){
            $where[] = ['e.name','like', '%'.$param['enginame'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['bill.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['modeid']) && !empty($param['modeid'])){
            $where[] = ['bill.modeid','=', $param['modeid']];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $seadt = explode(' - ', $param['created_at']);
            $where[] = ['bill.created_at','between', [$seadt[0].' 00:00:00', $seadt[1].' 23:59:59']];
        }



        $datatype = $request->param('datatype','wofa','string');
        //我发起的
        if($datatype == 'wofa'){
            $where[] = ['bill.created_user','=',$this->userinfo['id']];
            $data['data'] = FlowBillModel::alias('bill')
                ->field('bill.*,e.name as enginname')
                ->leftJoin('engin e','e.id=bill.enginid')
                ->where($where)
                ->order('bill.id DESC')
                ->paginate($param['pagesize'],false,['page'=>$param['page']])
                ->each(function ($item,$key){
                    return $item;
                })->toArray();
        }

        //我审核的
        if($datatype == 'woshen'){
            $where[] = ['shen.uid','=', $this->userinfo['id']];
            $data['data'] = FlowTaskShenModel::alias('shen')
                ->field('bill.*,e.name as enginname')
                ->join('flow_bill bill','bill.id=shen.billid')
                ->leftJoin('engin e','e.id=bill.enginid')
                ->where($where)
                ->group('bill.id')
                ->paginate($param['pagesize'],false,['page'=>$param['page']])
                ->each(function ($item,$key){
                    return $item;
                })->toArray();
        }

        //抄送我的
        if($datatype == 'chaowo'){
            $wherePromer = Db::raw("promer.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
            $data['data'] = FlowPromerModel::alias('promer')
                ->field('bill.*,e.name as enginname')
                ->join('flow_bill bill','promer.billid = bill.id')
                ->leftJoin('engin e','e.id=bill.enginid')
                ->where($where)
                ->where($wherePromer)
                ->group('bill.id')
                ->paginate($param['pagesize'],false,['page'=>$param['page']])
                ->each(function ($item,$key){
                    return $item;
                })->toArray();
        }

        //分享我的
        if($datatype == 'fenwo'){
            $whereShare = Db::raw("share.uid=".$this->userinfo['id']." or find_in_set(".$this->userinfo['id'].",domer)");
            $data['data'] = FlowShareModel::alias('share')
                ->field('bill.*,e.name as enginname')
                ->leftJoin('flow_bill bill','share.billid = bill.id')
                ->leftJoin('engin e','e.id=bill.enginid')
                ->where($where)
                ->where($whereShare)
                ->group('bill.id')
                ->paginate($param['pagesize'],false,['page'=>$param['page']])
                ->each(function ($item,$key){
                    return $item;
                })->toArray();
        }


        $data['modeidArr'] = (new FlowService())->get_flow_modename();
        $data['is_modeid'] = isset($param['modeid']) ? $param['modeid'] : '';
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function get_engin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|e.jian_unit|e.areas|e.ziyao|e.lian_unit|e.kuang|e.zhao_unit','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['engintype']) && !empty($param['engintype'])){
            $where[] = ['e.engintype','=', $param['engintype']];
        }
        if(isset($param['jian_unit']) && !empty($param['jian_unit'])){
            $where[] = ['e.jian_unit','like', '%'.$param['jian_unit'].'%'];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['e.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['e.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['ziyao']) && !empty($param['ziyao'])){
            $where[] = ['e.ziyao','like', '%'.$param['ziyao'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['lian_unit']) && !empty($param['lian_unit'])){
            $where[] = ['e.lian_unit','like', '%'.$param['lian_unit'].'%'];
        }
        if(isset($param['kuang']) && !empty($param['kuang'])){
            $where[] = ['e.kuang','like', '%'.$param['kuang'].'%'];
        }
        if(isset($param['zhao_unit']) && !empty($param['zhao_unit'])){
            $where[] = ['e.zhao_unit','like', '%'.$param['zhao_unit'].'%'];
        }
        if(isset($param['shikaidt']) && !empty($param['shikaidt'])){
            $where[] = ['e.shikaidt','between', explode(' - ', $param['shikaidt'])];
        }
        if(isset($param['shijundt']) && !empty($param['shijundt'])){
            $where[] = ['e.shijundt','between', explode(' - ', $param['shijundt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['engintype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_engintype'] = isset($param['engintype']) ? $param['engintype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('engin_zhaotype');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new EnginService())->get_engin_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        if(isset($param['isgodanno']) && !empty($param['isgodanno'])){
            if($param['isgodanno'] > 0){
                $where[] = ['e.godanid','=', 0];
            }
        }


        $data['data'] = EnginModel::alias('e')
            ->field('e.*,xiang.realname as man_xiang_name')
            ->leftJoin('user xiang','xiang.id=e.man_xiang')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['state_name'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['state_name_mob'] = (new EnginService())->get_engin_state_name($item['state'], $item['status']);
                $item['engintype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('engin_zhaotype', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';

                $item['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();
                if($item['shiinfo']){
                    $item['shiinfo']['faer_info'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$item['shiinfo']['faer']])->where(['isturn'=>1,'status'=>1])->find();
                    $item['shiinfo']['chenger_info'] = (new UserService())->get_users_part_arr($item['shiinfo']['chenger'], 'dept');
                }
                $item['budgetinfo'] = EnginBudgetModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->find();

//                $item['xiang_gonger_info'] = [];
//                $xiang = LabourXiangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'enginid'=>$item['id']])->find();
//                if($xiang){
//                    $item['xiang_gonger_info'] = LabourGongModel::field('id,name')->where('id','in',$xiang['gonger'])->select()->toArray();
//                }


                //本项目备用金余额【申请人=当前用户、关联项目=本项目的备用金余额的合计】
                $item['myjieyufee'] = ZhiJieModel::where(['enginid'=>$item['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'created_user'=>$this->userinfo['id']])->sum('yufee');


//                $item['jingcertinfo'] = '';
//                $jingcert = CertUseListModel::alias('list')
//                    ->join('cert_use use','use.id=list.useid')
//                    ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
//                    ->where(['use.isturn'=>1,'use.status'=>1])
//                    ->where(['use.enginid'=>$item['id']])
//                    ->where(['use.isreal'=>1,'list.place'=>'项目经理'])
//                    ->value('certid');
//                if($jingcert){
//                    $item['jingcertinfo'] = CertModel::where(['id'=>$jingcert])->find();
//                    $item['jingcertinfo']['person_name'] = PersonModel::where(['id'=>$item['jingcertinfo']['personid']])->value('name');
//                }
//
//                $item['shucertinfo'] = '';
//                $shucert = CertUseListModel::alias('list')
//                    ->join('cert_use use','use.id=list.useid')
//                    ->where(['use.comid'=>$this->userinfo['comid'],'use.deleted_user'=>0])
//                    ->where(['use.isturn'=>1,'use.status'=>1])
//                    ->where(['use.enginid'=>$item['id']])
//                    ->where(['use.isreal'=>1,'list.place'=>'项目技术负责人'])
//                    ->value('certid');
//                if($shucert){
//                    $item['shucertinfo'] = CertModel::where(['id'=>$shucert])->find();
//                    $item['shucertinfo']['person_name'] = PersonModel::where(['id'=>$item['shucertinfo']['personid']])->value('name');
//                }


                $cert_use = [];
                $useArr = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'isreal'=>1,'enginid'=>$item['id']])->find();
                if($useArr){
                    $certuselist = CertUseListModel::where(['useid'=>$useArr['id']])->select()->toArray();
                    foreach ($certuselist as $vvv){
                        $certInfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        $vvv['name'] = $certInfo['name'];
                        $vvv['number'] = $certInfo['number'];
                        $vvv['statueuse'] = $certInfo['statueuse'];
                        $vvv['major_name'] = str_replace('[]', ',', $certInfo['major']);
                        $vvv['person_name'] = PersonModel::where(['id'=>$certInfo['personid']])->value('name');
                        $vvv['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
                        $cert_use[] = $vvv;
                    }
                }
                $item['cert_use'] = $cert_use;

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_dealcom(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['deal.deleted_user','=', 0];
        $where[] = ['deal.comid','=',$this->userinfo['comid']];
        $where[] = ['deal.isturn','=',1];
        $where[] = ['deal.status','=',1];
        $isbian = $request->post('isbian', 0,  'int');

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['deal.name|deal.legaler|deal.lianer|deal.liantel|deal.ratenum|deal.unittel','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['deal.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['deal.type','=', $param['type']];
        }
        if(isset($param['nature']) && !empty($param['nature'])){
            $where[] = ['deal.nature','=', $param['nature']];
        }
        if(isset($param['legaler']) && !empty($param['legaler'])){
            $where[] = ['deal.legaler','like', '%'.$param['legaler'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['deal.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['liantel']) && !empty($param['liantel'])){
            $where[] = ['deal.liantel','like', '%'.$param['liantel'].'%'];
        }
        if(isset($param['taxtype']) && !empty($param['taxtype'])){
            $where[] = ['deal.taxtype','=', $param['taxtype']];
        }
        if(isset($param['ratenum']) && !empty($param['ratenum'])){
            $where[] = ['deal.ratenum','like', '%'.$param['ratenum'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['deal.status','=', $param['status']];
        }

        $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';

        $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
        $data['is_taxtype'] = isset($param['taxtype']) ? $param['taxtype'] : '';

        $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
        $data['is_nature'] = isset($param['nature']) ? $param['nature'] : '';

        $data['data'] = DealcomModel::alias('deal')
            ->field('deal.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('dealcom', 'deal'))
            ->order('deal.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($isbian){
                $item['type_name'] = (new OptService())->get_base_showname('cai_dealcom_type', $item['type']);
                $item['nature_name'] = (new OptService())->get_base_showname('cai_dealcom_nature', $item['nature']);
                $item['taxtype_name'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $item['taxtype']);
                $item['piaotype_name'] = (new OptService())->get_base_showname('cai_dealcom_piaotype', $item['piaotype']);

                //单位信息变更时，获取明细和附件信息
                $item['list'] = [];
                $item['files'] = [];
                if($isbian){
                    $item['list'] = DealcomListModel::where(['dealid'=>$item['id']])->select()->toArray();
                    $item['files'] = (new OptService())->get_file('dealcom', $item['id']);
                }

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_dealcom_bank(Request $request){
        $param = $request->post();
        get_page($param);

        $data['dealid'] = 0;
        $data['list'] = [];

        $name = $request->post('name', 'string', '');
        $dealinfo = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1,'name'=>$name])->find();
        if(!$dealinfo){
            return outputJson(2000,lang('code_2000'),$data);
        }

        $data['dealid'] = $dealinfo['id'];
        $list = DealcomListModel::where(['dealid'=>$dealinfo['id']])->order('isdea DESC')->select()->toArray();
        foreach ($list as &$item){
            $item['isdea_name'] = $item['isdea'] > 0 ? '默认' : '';
        }
        $data['list'] = $list;

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_shi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shi.deleted_user','=', 0];
        $where[] = ['shi.comid','=',$this->userinfo['comid']];
        $where[] = ['shi.isturn','=',1];
        $where[] = ['shi.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shi.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|shi.name|shi.number|shi.faer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['shi.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shi.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shihetype']) && !empty($param['shihetype'])){
            $where[] = ['shi.shihetype','=', $param['shihetype']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['shi.year','=', $param['year']];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['shi.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['chenger']) && !empty($param['chenger'])){
            $where[] = ['shi.chenger','=', $param['chenger']];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['shi.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['shi.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['shi.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['shi.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['zhidt']) && !empty($param['zhidt'])){
            $where[] = ['shi.zhidt','between', explode(' - ', $param['zhidt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shi.status','=', $param['status']];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['chenger'] = (new UserService())->get_chaninfo_all();
        $data['is_chenger'] = isset($param['chenger']) ? $param['chenger'] : '';

        $data['state'] = (new ContractService())->get_contract_shi_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
        $data['is_shihetype'] = isset($param['shihetype']) ? $param['shihetype'] : '';

        $data['data'] = ContractShiModel::alias('shi')
            ->field('shi.*,e.name as enginname,e.business,e.shijiemoney')
            ->join('engin e','e.id=shi.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_shi', 'shi'))
            ->order('shi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chenger_name'] = DeptModel::get_name_by_pk($item['chenger']);
                $item['shihetype_name'] = (new OptService())->get_base_showname('engin_shihetype', $item['shihetype']);

                $item['faer_info'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$item['faer']])->where(['isturn'=>1,'status'=>1])->find();
                $item['chenger_info'] = (new UserService())->get_users_part_arr($item['chenger'], 'dept');

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_budget(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bud.deleted_user','=','0'];
        $where[] = ['bud.comid','=',$this->userinfo['comid']];
        $where[] = ['bud.isturn','=',1];
        $where[] = ['bud.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['bud.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|bud.number|bud.title','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bud.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['bud.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = EnginBudgetModel::alias('bud')
            ->field('bud.*,e.name as enginname')
            ->join('engin e','e.id=bud.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_budget', 'bud'))
            ->order('bud.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_budget_list(Request $request){
        $param = $request->post();
        $budgetid = $request->param('budgetid', 0, 'int');


        $where[] = ['budgetid','=',$budgetid];
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['budtype']) && !empty($param['budtype'])){
            $where[] = ['budtype','=', $param['budtype']];
        }

        $data['budtype'] = (new EnginService())->get_engin_budget_budtype();
        $data['is_budtype'] = isset($param['budtype']) ? $param['budtype'] : '';

        $data['data'] = EnginBudgetListModel::where($where)
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['budtype_name'] = (new EnginService())->get_engin_budget_budtype_name($item['budtype']);

                $item['yiqiannum'] = ContractCaiListModel::alias('list')->join('contract_cai cai','cai.id=list.caiid')
                    ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'list.listid'=>$item['id']])
                    ->sum('list.num');
                $item['yiqianmoney'] = ContractCaiListModel::alias('list')->join('contract_cai cai','cai.id=list.caiid')
                    ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'list.listid'=>$item['id']])
                    ->sum('list.money');
                $item['yiqianjun'] = 0;
                if($item['yiqiannum'] != 0){
                    $item['yiqianjun'] = round($item['yiqianmoney']/$item['yiqiannum'], 2);
                }
                $item['shengnum'] = round($item['num']-$item['yiqiannum'], 2);
                $item['shengmoney'] = round($item['money']-$item['yiqianmoney'], 2);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_cai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|cai.name|cai.number|cai.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cai.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['caitype']) && !empty($param['caitype'])){
            $where[] = ['cai.caitype','=', $param['caitype']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['cai.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['caishu']) && !empty($param['caishu'])){
            $where[] = ['cai.caishu','=', $param['caishu']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['cai.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['cai.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cai.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['cai.qiandt','between', explode(' - ', $param['qiandt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new ContractService())->get_contract_shi_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['caitype'] = (new ContractService())->get_contract_cai_caitype();
        $data['is_caitype'] = isset($param['caitype']) ? $param['caitype'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['caishu'] = (new ContractService())->get_contract_cai_caishu();
        $data['is_caishu'] = isset($param['caishu']) ? $param['caishu'] : '';



        $data['data'] = ContractCaiModel::alias('cai')
            ->field('cai.*,e.name as enginname,e.business')
            ->join('engin e','e.id=cai.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_cai', 'cai'))
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['caitype_name'] = (new ContractService())->get_contract_cai_caitype_name($item['caitype']);
                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
                $item['gonger_info'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$item['gonger']])->where(['isturn'=>1,'status'=>1])->find();
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_cai_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['caiid','=',$param['caiid']];
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }


        $data['data'] = ContractCaiListModel::where($where)
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['hanghao'] = $key+1;
                $item['listid_name'] = EnginBudgetListModel::where(['id'=>$item['listid']])->value('name');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_cai_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['jie.enginid','=', $param['enginid']];
        }
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['jie.caiid','=', $param['caiid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|jie.number|jie.title|cai.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['jie.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['caitype']) && !empty($param['caitype'])){
            $where[] = ['cai.caitype','=', $param['caitype']];
        }
        if(isset($param['jietype']) && !empty($param['jietype'])){
            $where[] = ['jie.jietype','=', $param['jietype']];
        }
        if(isset($param['jiedt']) && !empty($param['jiedt'])){
            $where[] = ['jie.jiedt','between', explode(' - ', $param['jiedt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['jie.status','=', $param['status']];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['jietype'] = (new ContractService())->get_contract_shi_jie_jietype();
        $data['is_jietype'] = isset($param['jietype']) ? $param['jietype'] : '';

        $data['caitype'] = (new ContractService())->get_contract_cai_caitype();
        $data['is_caitype'] = isset($param['caitype']) ? $param['caitype'] : '';


        $data['data'] = ContractCaiJieModel::alias('jie')
            ->field('jie.*,e.name as enginname,e.business,cai.name as cainame,cai.caitype,cai.gonger')
            ->join('engin e','e.id=jie.enginid')
            ->join('contract_cai cai','cai.id=jie.caiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_cai_jie', 'jie'))
            ->order('jie.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['caitype_name'] = (new ContractService())->get_contract_cai_caitype_name($item['caitype']);
                $item['gonger_info'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$item['gonger']])->where(['isturn'=>1,'status'=>1])->find();
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_buy(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['buy.deleted_user','=', 0];
        $where[] = ['buy.comid','=',$this->userinfo['comid']];
        $where[] = ['buy.isturn','=',1];
        $where[] = ['buy.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['buy.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|buy.title|buy.number|buy.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['buy.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['buy.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['buytype']) && !empty($param['buytype'])){
            $where[] = ['buy.buytype','=', $param['buytype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['buy.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['buy.dt','between', explode(' - ', $param['dt'])];
        }


        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['buy.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['buytype'] = (new ContractService())->get_contract_buy_buytype();
        $data['is_buytype'] = isset($param['buytype']) ? $param['buytype'] : '';



        $data['data'] = ContractBuyModel::alias('buy')
            ->field('buy.*,e.name as enginname,e.business')
            ->join('engin e','e.id=buy.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_buy', 'buy'))
            ->order('buy.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['buytype_name'] = (new ContractService())->get_contract_buy_buytype_name($item['buytype']);
                $item['gonger_info'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$item['gonger']])->where(['isturn'=>1,'status'=>1])->find();
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_contract_buy_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['buyid','=',$param['buyid']];
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }


        $data['data'] = ContractBuyListModel::where($where)
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['hanghao'] = $key+1;
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_piao_shou_list(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['shou.deleted_user','=', 0];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|shou.title|shou.number|shou.shouer|list.gonger|shou.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['shou.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shou.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['shouer']) && !empty($param['shouer'])){
            $where[] = ['shou.shouer','like', '%'.$param['shouer'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['list.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['shou.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shou.status','=', $param['status']];
        }

        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['list.piaotype','=', $param['piaotype']];
        }

        if(isset($param['isdi']) && !empty($param['isdi'])){
            if($param['isdi'] == 'no'){
                $where[] = ['list.dikouid','=', 0];
            }
        }

        $data['data'] = PiaoShouListModel::alias('list')
            ->field('shou.*,e.name as enginname,bill.sericnum
            ,list.id as listid,list.caiid,list.gonger,list.dt as listdt,list.nums,list.piaotype,list.fee as listfee,list.rate as listrate
            ,list.ratefee as listratefee,list.money as listmoney,list.explain as listexplain,list.yongtype,list.dikouid')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->join('engin e','e.id=shou.enginid')
            ->leftJoin('flow_bill bill','bill.id=shou.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('piao_shou', 'shou'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['hanghao'] = $key+1;
                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
                $item['piaotype_name'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                $item['yongtype_name'] = (new PiaoService())->get_piao_shou_yongtype_name($item['yongtype']);
                $item['dikouid_name'] = $item['dikouid'] > 0 ? '是' : '否';

                $item['isshen'] = false;
                $isshen = ZhiJintuiListModel::alias('list')
                    ->join('zhi_jintui zhi','zhi.id=list.zhiid')
                    ->where(['zhi.comid'=>$this->userinfo['comid'],'zhi.deleted_user'=>0])
                    ->where(['zhi.isturn'=>1,'zhi.status'=>0])
                    ->where(['list.piaoid'=>$item['listid']])
                    ->find();
                if($isshen){
                    $item['isshen'] = true;
                    $item['dikouid_name'] .= '(审核中)';
                }

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_stuff_ru(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=',1];
        $where[] = ['ru.enginid','=',$param['enginid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.name|cai.number|ru.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }

        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ru.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['ru.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }



        $data['data'] = StuffRuModel::alias('ru')
            ->field('ru.*,e.name as enginname,cai.name as cainame,buy.title as buytitle')
            ->join('engin e','e.id=ru.enginid')
            ->leftJoin('contract_cai cai','cai.id=ru.caiid')
            ->leftJoin('contract_buy buy','buy.id=ru.buyid')
            ->where($where)
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['gonger_info'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$item['gonger']])->where(['isturn'=>1,'status'=>1])->find();
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_stuff_ru_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];
        $where[] = ['ru.enginid','=', $param['enginid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['list.name|ru.number|ru.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ru.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['ru.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['ru.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listunit']) && !empty($param['listunit'])){
            $where[] = ['list.unit','like', '%'.$param['listunit'].'%'];
        }
        if(isset($param['listprice']) && !empty($param['listprice'])){
            $where[] = ['list.price','like', '%'.$param['listprice'].'%'];
        }

        if(isset($param['isku']) && !empty($param['isku'])){
            if($param['isku'] == 1){
                $where[] = ['list.cunnum','>', 0];
            }elseif($param['isku'] == 2){
                $where[] = ['list.cunnum','=', 0];
            }

        }

        $data['data'] = StuffRuListModel::alias('list')
            ->field('ru.*,e.name as enginname,list.id as listid,list.name as listname,list.unit as listunit
            ,list.price as listprice,list.num as listnum,list.money as listmoney
            ,list.chunum,list.tuinum,list.cunnum,list.cunmoney')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->where($where)
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['hanghao'] = $key+1;
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_godan_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];
        $where[] = ['enginid','=',$param['enginid']];

        $godanid = EnginGodanModel::where($where)->value('id');
        $whereSou = "godanid=".$godanid;

        $where222 = [];
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where222[] = ['num|name|xiang','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['num']) && !empty($param['num'])){
            $where222[] = ['num','like', '%'.$param['num'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where222[] = ['name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['xiang']) && !empty($param['xiang'])){
            $where222[] = ['xiang','like', '%'.$param['xiang'].'%'];
        }

        $list = EnginGodanListModel::where($whereSou)->where($where222)->select()->toArray();
        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_payment_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jie.deleted_user','=', 0];
        $where[] = ['jie.comid','=',$this->userinfo['comid']];
        $where[] = ['jie.created_user','=',$this->userinfo['id']];
        $where[] = ['jie.isturn','=',1];
        $where[] = ['jie.status','=',1];

        if(isset($param['ismy']) && !empty($param['ismy'])){
            $where[] = ['jie.gonger','=', $this->userinfo['realname']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jie.number|jie.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jie.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['incyong']) && !empty($param['incyong'])){
            $where[] = ['jie.incyong','like', '%'.$param['incyong'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['jie.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['jie.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['jie.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['jie.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['jie.bankname','like', '%'.$param['bankname'].'%'];
        }


        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';


        $list = [];
        $info = PaymentJieModel::alias('jie')
            ->field('jie.*')
            ->where($where)
            ->order('jie.id DESC')
            ->select()->toArray();

        foreach ($info as $item){
            $item['incdaotype'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
            $item['accmoney'] = AccountListModel::where(['type'=>13,'mid'=>$item['id']])->sum('money');

            $item['difee'] = PaymentBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'jieid'=>$item['id']])->where('status','in',[0,1])->sum('difee');
            $item['difee'] += PaymentGongModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'jieid'=>$item['id']])->where('status','in',[0,1])->sum('difee');

            $huanidArr = IncomeHuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'jieid'=>$item['id']])->column('id');
            $item['huanfee'] = AccountListModel::where(['type'=>14])->where('mid','in', $huanidArr)->sum('money');

            $item['daihuan'] = $item['accmoney'] - $item['difee'] - $item['huanfee'];

            if($item['daihuan'] > 0){
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_zhi_jie(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['zhi.deleted_user','=','0'];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','=',1];

        if(isset($param['ismy']) && !empty($param['ismy'])){
            $where[] = ['zhi.created_user','=',$this->userinfo['id']];
            $where[] = ['zhi.yufee','>',0];
        }
        if(isset($param['noengin']) && !empty($param['noengin'])){
            $where[] = ['zhi.enginid','=', 0];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zhi.enginid','=', $param['enginid']];
        }



        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|zhi.zhai|zhi.duier','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['zhi.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['zhi.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['feitype']) && !empty($param['feitype'])){
            $where[] = ['zhi.feitype','=', $param['feitype']];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['zhi.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['feitype'] = (new UserService())->get_option_fei_slet(['wlk_byj']);
        $data['is_feitype'] = isset($param['feitype']) ? $param['feitype'] : '';

        $data['data'] = ZhiJieModel::alias('zhi')
            ->field('zhi.*,e.name as enginname')
            ->leftJoin('engin e','e.id=zhi.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('zhi_jie', 'zhi'))
            ->order('zhi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('zhi_jie');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_bond_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shou.deleted_user','=','0'];
        $where[] = ['shou.comid','=',$this->userinfo['comid']];
        $where[] = ['shou.isturn','=',1];
        $where[] = ['shou.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shou.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|shou.duier|shou.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['shou.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['shou.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['shou.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['shou.dt','between', explode(' - ', $param['dt'])];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['data'] = BondShouModel::alias('shou')
            ->field('shou.*,e.name as enginname,acc.name as accname,acc.accnum')
            ->join('engin e','e.id=shou.enginid')
            ->leftJoin('account acc','acc.id=shou.accid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bond_shou', 'shou'))
            ->order('shou.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_bond_jiao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['zhi.deleted_user','=','0'];
        $where[] = ['zhi.comid','=',$this->userinfo['comid']];
        $where[] = ['zhi.isturn','=',1];
        $where[] = ['zhi.status','=',1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['zhi.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|zhi.zhai|zhi.duier','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['zhi.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['zhi.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['zhi.incdaotype','=', $param['incdaotype']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['zhi.status','=', $param['status']];
        }

        $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
        $data['is_incdaotype'] = isset($param['incdaotype']) ? $param['incdaotype'] : '';

        $data['data'] = BondJiaoModel::alias('zhi')
            ->field('zhi.*,e.name as enginname')
            ->join('engin e','e.id=zhi.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bond_jiao', 'zhi'))
            ->order('zhi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                $item['shouidinfo'] = BondShouModel::where(['id'=>$item['shouid']])->find();
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                $item['incdaotype_name'] = (new OptService())->get_base_showname('cai_income_incdaotype', $item['incdaotype']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_person(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['p.deleted_user','=','0'];
        $where[] = ['p.comid','=',$this->userinfo['comid']];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['p.chanid','in', (new MenuService())->get_chanid_sons($param['chanid'])];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['p.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['p.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['idcard']) && !empty($param['idcard'])){
            $where[] = ['p.idcard','like', '%'.$param['idcard'].'%'];
        }
        if(isset($param['sex']) && !empty($param['sex'])){
            $where[] = ['p.sex','=', $param['sex']];
        }
        if(isset($param['xueli']) && !empty($param['xueli'])){
            $where[] = ['p.xueli','=', $param['xueli']];
        }
        if(isset($param['major']) && !empty($param['major'])){
            $where[] = ['p.major','like', '%'.$param['major'].'%'];
        }
        if(isset($param['nature']) && !empty($param['nature'])){
            $where[] = ['p.nature','=', $param['nature']];
        }
        if(isset($param['intodt']) && !empty($param['intodt'])){
            $where[] = ['p.intodt','between', explode(' - ', $param['intodt'])];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['p.state','=', $param['state']];
        }
        if(isset($param['outdt']) && !empty($param['outdt'])){
            $where[] = ['p.outdt','between', explode(' - ', $param['outdt'])];
        }
        if(isset($param['social']) && !empty($param['social'])){
            $where[] = ['p.social','=', $param['social']];
        }
        if(isset($param['socarea']) && !empty($param['socarea'])){
            $where[] = ['p.socarea','like', '%'.$param['socarea'].'%'];
        }
        if(isset($param['socstartdt']) && !empty($param['socstartdt'])){
            $where[] = ['p.socstartdt','between', explode(' - ', $param['socstartdt'])];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['xueli'] = (new CertService())->get_person_xueli();
        $data['is_xueli'] = isset($param['xueli']) ? $param['xueli'] : '';

        $data['nature'] = (new CertService())->get_person_nature();
        $data['is_nature'] = isset($param['nature']) ? $param['nature'] : '';

        $data['social'] = (new CertService())->get_person_social();
        $data['is_social'] = isset($param['social']) ? $param['social'] : '';

        $data['state'] = (new CertService())->get_person_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $wherePage = [];
        if($this->userinfo['token_roletype'] != 1){
            if(isset($param['parentpage']) && $param['parentpage'] == 'cert'){
                $userChanid = (new UserService())->get_user_chanid();
                $chanid_son = (new UserService())->get_chansonid($userChanid);
                $wherePage[] = ['p.chanid','in', $chanid_son];
            }
        }

        $data['data'] = PersonModel::alias('p')
            ->field('p.*')
            ->where($where)
            ->where($wherePage)
            ->where((new MenuService())->get_miss_where('person', 'p'))
            ->order('p.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['deptid_name'] = DeptModel::get_name_by_pk($item['deptid']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['age'] = (new CertService())->get_age_by_idcard($item['idcard']);
                $item['socnum'] = (new CertService())->get_person_social_month($item['state'],$item['socstartdt'],$item['socenddt']);
                $item['gangid_name'] = UserGangModel::get_name_by_ids($item['gangid'],' / ');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_cert(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cert.deleted_user','=','0'];
        $where[] = ['cert.comid','=',$this->userinfo['comid']];
        $where[] = ['cert.state','not in', ['转出','注销']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cert.name|person.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['nature']) && !empty($param['nature'])){
            $where[] = ['person.nature','=',$param['nature']];
        }
        if(isset($param['person_name']) && !empty($param['person_name'])){
            $where[] = ['person.name','like', '%'.$param['person_name'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cert.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cert.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['major']) && !empty($param['major'])){
            $where[] = ['cert.major','like', '%'.$param['major'].'%'];
        }
        if(isset($param['issue_unit']) && !empty($param['issue_unit'])){
            $where[] = ['cert.issue_unit','like', '%'.$param['issue_unit'].'%'];
        }
        if(isset($param['issue_dt']) && !empty($param['issue_dt'])){
            $where[] = ['cert.issue_dt','between', explode(' - ', $param['issue_dt'])];
        }
        if(isset($param['expiry_dt']) && !empty($param['expiry_dt'])){
            $expiry_dt = explode(' - ', $param['expiry_dt']);
            $ids = [];
            $c_data = CertModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->field('id,expiry_dt')->select()->toArray();
            foreach ($c_data as $item){
                $expiry_dtArr = explode('[]',$item['expiry_dt']);
                if($expiry_dtArr){
                    foreach ($expiry_dtArr as $dt){
                        if($dt >= $expiry_dt[0] && $dt <= $expiry_dt[1]){
                            $ids[] = $item['id'];
                        }
                    }
                }
            }
            $ids = array_unique($ids);
            $where[] = ['cert.id','in',$ids];
        }
        if(isset($param['form']) && !empty($param['form'])){
            $where[] = ['cert.form','=', $param['form']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cert.state','in', $param['state']];
        }
        if(isset($param['electron_dt']) && !empty($param['electron_dt'])){
            $where[] = ['cert.electron_dt','between', explode(' - ', $param['electron_dt'])];

        }
        if(isset($param['inspect_dt']) && !empty($param['inspect_dt'])){
            $where[] = ['cert.inspect_dt','between', explode(' - ', $param['inspect_dt'])];
        }
        if(isset($param['custodianid']) && !empty($param['custodianid'])){
            $where[] = ['u.realname','like', '%'.$param['custodianid'].'%'];
        }


        if(isset($param['statueuse']) && !empty($param['statueuse'])){
            $where[] = ['cert.statueuse','=', $param['statueuse']];
        }
        if(isset($param['goes']) && !empty($param['goes'])){
            $where[] = ['cert.goes','=', $param['goes']];
        }
        if(isset($param['use']) && !empty($param['use'])){
            $where[] = ['cert.use','=', $param['use']];
        }
        if(isset($param['person_state']) && !empty($param['person_state'])){
            $where[] = ['person.state','=', $param['person_state']];
        }
        if(isset($param['out_dt']) && !empty($param['out_dt'])){
            $where[] = ['cert.out_dt','between', explode(' - ', $param['out_dt'])];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['cert.type','=', $param['type']];
        }
        if(isset($param['personid']) && !empty($param['personid'])){
            $where[] = ['cert.personid','=',$param['personid']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['cert.chanid','in', (new MenuService())->get_chanid_sons($param['chanid'])];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['form'] = (new CertService())->get_cert_form();
        $data['is_form'] = isset($param['form']) ? $param['form'] : '';

        $data['person_state'] = (new CertService())->get_person_state();
        $data['is_person_state'] = isset($param['person_state']) ? $param['person_state'] : '';

        $data['state'] = (new CertService())->get_cert_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['statueuse'] = (new CertService())->get_cert_statueuse();
        $data['is_statueuse'] = isset($param['statueuse']) ? $param['statueuse'] : '';

        $data['goes'] = (new CertService())->get_cert_goes();
        $data['is_goes'] = isset($param['goes']) ? $param['goes'] : '';

        $data['nature'] = (new CertService())->get_person_nature();
        $data['is_nature'] = isset($param['nature']) ? $param['nature'] : '';


        if(isset($param['isborrow']) && !empty($param['isborrow'])){
            if($param['isborrow'] == 'borrow'){
                $where[] = ['cert.state','=','正常'];
                $where[] = ['cert.goes','=','在册'];
                $where[] = ['cert.form','=','纸质原件'];
            }
            if($param['isborrow'] == 'down'){
                $where[] = ['cert.state','=','正常'];
            }
        }

        $data['data'] = CertModel::alias('cert')
            ->field('cert.*,u.realname as custodian_name,person.name as person_name,person.state as person_state,person.nature')
            ->leftjoin('person person','person.id=cert.personid')
            ->leftJoin('user u','u.id = cert.custodianid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('cert', 'cert'))
            ->order('cert.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['expiry_dt_name'] = str_replace('[]','，',$item['expiry_dt']);
                $item['major_name'] = str_replace('[]','，',$item['major']);
                $major = array_filter(explode('[]',$item['major']));
                $item['major_str'] = implode(' / ',$major);
                $item['major'] = '<div style="line-height: 20px">'.str_replace('[]','<br/>',$item['major']).'</div>';

                $expiry_dtArr = explode('[]',$item['expiry_dt']);
                $arr = [];
                foreach($expiry_dtArr as $v){
                    if(empty($v)){
                        $arr[] = $v;
                    }else{
                        if($v < date('Y-m-d')){
                            $arr[] = '<span style="color:red;">'.$v.'</span>';
                        }else{
                            $arr[] = $v;
                        }
                    }
                }
                $item['expiry_dt'] = '<div style="line-height: 20px">'.implode('<br/>',$arr).'</div>';

                $item['custodian_name'] = UserModel::get_realname_by_pk($item['custodianid']);
                $item['type_name'] = (new OptService())->get_base_showname('cert_type', $item['type']);
                $item['store_addr'] = (new OptService())->get_base_showname('cert_store_addr',$item['store_addr']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_cert_company()
    {
        $param = input('post.');
        get_page($param);

        $where[] = ['cert.deleted_user','=','0'];
        $where[] = ['cert.pid','=','0'];
        $where[] = ['cert.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cert.name|cert.number|cert.issue_unit|cert.zinumber|u.realname','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['cert.chanid','=', $param['chanid']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cert.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cert.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['form']) && !empty($param['form'])){
            $where[] = ['cert.form','eq', $param['form']];
        }
        if(isset($param['goes']) && !empty($param['goes'])){
            $where[] = ['cert.goes','=', $param['goes']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cert.state','eq', $param['state']];
        }
        if(isset($param['issue_unit']) && !empty($param['issue_unit'])){
            $where[] = ['cert.issue_unit','like', '%'.$param['issue_unit'].'%'];
        }
        if(isset($param['issue_dt']) && !empty($param['issue_dt'])){
            $where[] = ['cert.issue_dt','=', $param['issue_dt']];
        }
        if(isset($param['expiry_dt']) && !empty($param['expiry_dt'])){
            $where[] = ['cert.expiry_dt','like', '%'.$param['expiry_dt'].'%'];
        }
        if(isset($param['inspect_dt']) && !empty($param['inspect_dt'])){
            $where[] = ['cert.inspect_dt','=', $param['inspect_dt']];
        }
        if(isset($param['zinumber']) && !empty($param['zinumber'])){
            $where[] = ['cert.zinumber','like', '%'.$param['zinumber'].'%'];
        }
        if(isset($param['custodianid']) && !empty($param['custodianid'])){
            $where[] = ['u.realname','like', '%'.$param['custodianid'].'%'];
        }
        if(isset($param['store_addr']) && !empty($param['store_addr'])){
            $where[] = ['cert.store_addr','eq', $param['store_addr']];
        }

        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['cert.type','eq', $param['type']];
        }


        if(isset($param['isborrow']) && !empty($param['isborrow'])){
            if($param['isborrow'] == 'borrow'){
                $where[] = ['cert.state','=','正常'];
                $where[] = ['cert.goes','=','在册'];
                $where[] = ['cert.form','=','纸质原件'];
            }
            if($param['isborrow'] == 'down'){
                $where[] = ['cert.state','=','正常'];
            }
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['form'] = (new CertService())->get_cert_company_form();
        $data['is_form'] = isset($param['form']) ? $param['form'] : '';

        $data['state'] = (new CertService())->get_cert_company_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['goes'] = (new CertService())->get_cert_company_goes();
        $data['is_goes'] = isset($param['goes']) ? $param['goes'] : '';

        $data['store_addr'] = (new OptService())->get_base_info('cert_store_addr');
        $data['is_store_addr'] = isset($param['store_addr']) ? $param['store_addr'] : '';

        $data['data'] = CertcompanyModel::alias('cert')
            ->leftJoin('user u','u.id = cert.custodianid')
            ->field('cert.*,u.realname as custodian_name')
            ->where($where)
            ->order('cert.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['custodian_name'] = UserModel::get_realname_by_pk($item['custodianid']);
                $item['chan_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['type_name'] = (new OptService())->get_base_showname('cert_company_type', $item['type']);
                $item['store_addr'] = (new OptService())->get_base_showname('cert_store_addr', $item['store_addr']);
                return $item;
            })->toArray();

        $old_data = $data['data']['data'];
        $now_data = [];
        foreach ($old_data as $item){
            $now_data[] = $item;
            $children = CertcompanyModel::where(['deleted_user'=>0,'pid'=>$item['id']])->select()->toArray();
            foreach ($children as $v){
                $v['custodian_name'] = UserModel::get_realname_by_pk($v['custodianid']);
                $v['chan_name'] = DeptModel::get_name_by_pk($v['chanid']);
                $v['type_name'] = (new OptService())->get_base_showname('cert_company_type', $v['type']);
                $v['store_addr'] = (new OptService())->get_base_showname('cert_store_addr', $v['store_addr']);
                $now_data[] = $v;
            }
        }
        $data['data']['data'] = $now_data;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_seal(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['seal.deleted_user','=', 0];
        $where[] = ['seal.comid','=',$this->userinfo['comid']];

        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['seal.state','=', $param['state']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['seal.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['sealtype']) && !empty($param['sealtype'])){
            $where[] = ['seal.sealtype','=', $param['sealtype']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['seal.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['seal.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baoname']) && !empty($param['baoname'])){
            $where[] = ['bao.realname','like', '%'.$param['baoname'].'%'];
        }


        $data['sealtype'] = (new OptService())->get_base_info('qi_seal_sealtype');
        $data['is_sealtype'] = isset($param['sealtype']) ? $param['sealtype'] : '';


        $data['data'] = SealModel::alias('seal')
            ->field('seal.*,bao.realname as baoname')
            ->leftJoin('user bao','bao.id=seal.baoid')
            ->where($where)
            ->order('seal.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['sealtype'] = (new OptService())->get_base_showname('qi_seal_sealtype', $item['sealtype']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_dang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['dang.deleted_user','=', 0];
        $where[] = ['dang.comid','=',$this->userinfo['comid']];
        $where[] = ['dang.isjie','=','是'];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['dang.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['dangtype']) && !empty($param['dangtype'])){
            $where[] = ['dang.dangtype','=', $param['dangtype']];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['dang.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['num']) && !empty($param['num'])){
            $where[] = ['dang.num','like', '%'.$param['num'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['dang.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['dang.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['dang.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['dangcun']) && !empty($param['dangcun'])){
            $where[] = ['dang.dangcun','=', $param['dangcun']];
        }



        $data['dangtype'] = (new OptService())->get_base_info('qi_dang_dangtype');
        $data['is_dangtype'] = isset($param['dangtype']) ? $param['dangtype'] : '';

        $data['dangcun'] = (new OptService())->get_base_info('qi_dang_dangcun');
        $data['is_dangcun'] = isset($param['dangcun']) ? $param['dangcun'] : '';


        $data['data'] = DangModel::alias('dang')
            ->field('dang.*')
            ->where($where)
            ->order('dang.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['dangtype'] = (new OptService())->get_base_showname('qi_dang_dangtype', $item['dangtype']);
                $item['dangcun'] = (new OptService())->get_base_showname('qi_dang_dangcun', $item['dangcun']);

                $item['jienum'] =  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1,'jie.isjie'=>1])
                    ->where("list.fandt != '' and list.fandt is not null")
                    ->where('list.dangid','=',$item['id'])
                    ->sum('list.num');
                $item['jienum'] +=  DangJieListModel::alias('list')
                    ->join('dang_jie jie','jie.id=list.jieid')
                    ->where(['jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                    ->where('list.dangid','=',$item['id'])
                    ->sum('list.num');

                $item['kejie'] = $item['num'] - $item['jienum'];

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_wu_cai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['cai.flowuser|cai.flowdept|cai.number|cai.title|cai.gonger|cai.gongtel|cai.money','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['cai.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['flowdept']) && !empty($param['flowdept'])){
            $where[] = ['cai.flowdept','like', '%'.$param['flowdept'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['cai.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['gongtel']) && !empty($param['gongtel'])){
            $where[] = ['cai.gongtel','like', '%'.$param['gongtel'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }


        $data['data'] = WuCaiModel::alias('cai')
            ->field('cai.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('wu_cai', 'cai'))
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['list'] = WuCaiListModel::where(['caiid'=>$item['id']])->select()->toArray();
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_wu_mingxi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['list.name|list.unit','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['list.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['unit']) && !empty($param['unit'])){
            $where[] = ['list.unit','like', '%'.$param['unit'].'%'];
        }


        $data['data'] = WuRuListModel::alias('list')
            ->field('list.*,sum(num) as runum')
            ->join('wu_ru ru','ru.id=list.ruid')
            ->where($where)
            ->where((new MenuService())->get_opt_where('wu_mingxi', 'ru'))
            ->order('list.id DESC')
            ->group('list.name,list.unit')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chunum'] = WuChuModel::where(['comid'=>$this->userinfo['id'],'name'=>$item['name'],'unit'=>$item['unit']])->sum('num');
                $item['chunum'] += WuLingListModel::alias('list')
                    ->join('wu_ling ling','ling.id=list.lingid')
                    ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->sum('list.num');
                $item['kucun'] = round($item['runum']-$item['chunum'], 2);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_account(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['acc.deleted_user','=', 0];
        $where[] = ['acc.comid','=',$this->userinfo['comid']];
        $where[] = ['acc.status','=',1];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['acc.name|acc.accname|acc.accnum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['acc.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['accnum']) && !empty($param['accnum'])){
            $where[] = ['acc.accnum','like', '%'.$param['accnum'].'%'];
        }
        if(isset($param['acctype']) && !empty($param['acctype'])){
            $where[] = ['acc.acctype','=', $param['acctype']];
        }
        if(isset($param['accmoney']) && !empty($param['accmoney'])){
            $where[] = ['acc.accmoney','like', '%'.$param['accmoney'].'%'];
        }
        if(isset($param['accdt']) && !empty($param['accdt'])){
            $where[] = ['acc.accdt','between', explode(' - ', $param['accdt'])];
        }


        $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
        $data['is_acctype'] = isset($param['acctype']) ? $param['acctype'] : '';




        $data['data'] = AccountModel::alias('acc')
            ->field('acc.*')
            ->where($where)
            ->where((new MenuService())->get_opt_where('account', 'acc'))
            ->order('acc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $item['acctype']);
                $item['chanid_name'] = DeptModel::where(['id'=>$item['chanid']])->value('name');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_xun(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['xun.deleted_user','=', 0];
        $where[] = ['xun.comid','=',$this->userinfo['comid']];
        $where[] = ['xun.isturn','=',1];
        $where[] = ['xun.status','=',1];
        $where[] = ['xun.enginid','=',$param['enginid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['xun.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['xun.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['xuntype']) && !empty($param['xuntype'])){
            $where[] = ['xun.xuntype','=', $param['xuntype']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['xun.dt','between', explode(' - ', $param['dt'])];
        }

        $wherexundept = '';
        if(isset($param['xundept']) && !empty($param['xundept'])){
            $wherexundept = Db::raw("find_in_set(".$param['xundept'].",xundept)");
        }

        $wherexuner = '';
        if(isset($param['xuner']) && !empty($param['xuner'])){
            $wherexuner = Db::raw("find_in_set(".$param['xuner'].",xuner)");
        }

        $data['xuntype'] = (new OptService())->get_base_info('scene_xuntype');
        $data['is_xuntype'] = isset($param['xuntype']) ? $param['xuntype'] : '';

        $data['data'] = EnginXunModel::alias('xun')
            ->field('xun.*,e.name as enginname')
            ->join('engin e','e.id=xun.enginid')
            ->where($where)
            ->where($wherexundept)
            ->where($wherexuner)
            ->order('xun.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['xuntype'] = (new OptService())->get_base_showname('scene_xuntype', $item['xuntype']);
                $item['xundept_name'] = DeptModel::get_name_by_ids($item['xundept']);
                $item['xuner_name'] = UserModel::get_realname_by_ids($item['xuner']);

                $list = EnginXunListModel::where(['xunid'=>$item['id']])->select()->toArray();
                foreach ($list as &$vv){
                    $vv['filearr'] = FileModel::where('id','in',$vv['files'])->select()->toArray();
                }
                $item['list'] = $list;


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_xun_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['xunid','=', $param['xunid']];
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['name','like', '%'.$param['keyword'].'%'];
        }

        $data['data'] = EnginXunListModel::where($where)
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_engin_jian(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['jian.deleted_user','=', 0];
        $where[] = ['jian.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['jian.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['jian.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['jiantype']) && !empty($param['jiantype'])){
            $where[] = ['jian.jiantype','=', $param['jiantype']];
        }

        $data['jiantype'] = (new OptService())->get_base_info('scene_jiantype');
        $data['is_jiantype'] = isset($param['jiantype']) ? $param['jiantype'] : '';

        $data['data'] = EnginJianModel::alias('jian')
            ->field('jian.*')
            ->where($where)
            ->order('jian.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['jiantype'] = (new OptService())->get_base_showname('scene_jiantype', $item['jiantype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_piao_kai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kai.deleted_user','=', 0];
        $where[] = ['kai.comid','=',$this->userinfo['comid']];
        $where[] = ['kai.isturn','=',1];
        $where[] = ['kai.status','=',1];
        $where[] = ['kai.enginid','=',$param['enginid']];




        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|kai.number|kai.duier|kai.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['kai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['piaotype']) && !empty($param['piaotype'])){
            $where[] = ['kai.piaotype','=', $param['piaotype']];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kai.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['ratenum']) && !empty($param['ratenum'])){
            $where[] = ['kai.ratenum','like', '%'.$param['ratenum'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['kai.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['kai.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['unitaddr']) && !empty($param['unitaddr'])){
            $where[] = ['kai.unitaddr','like', '%'.$param['unitaddr'].'%'];
        }
        if(isset($param['phone']) && !empty($param['phone'])){
            $where[] = ['kai.phone','like', '%'.$param['phone'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kai.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['kai.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['kai.status','=', $param['status']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';




        $data['piaotype'] = (new PiaoService())->get_kai_piaotype();
        $data['is_piaotype'] = isset($param['piaotype']) ? $param['piaotype'] : '';


        $whereDts = "kai.dt is not null and kai.dt != '' and kai.dt != '0000-00-00'";

        $data['data'] = PiaokaiModel::alias('kai')
            ->field('kai.*,e.name as enginname')
            ->join('engin e','e.id=kai.enginid')
            ->where($where)
            ->where($whereDts)
            ->order('kai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['piaotype'] = (new PiaoService())->get_kai_piaotype_name($item['piaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_speed_zong_list(Request $request){
        $param = $request->post();
        get_page($param);
        $zong = SpeedZongModel::where(['deleted_user'=>0,'enginid'=>$param['enginid']])->find();

        $where[] = ['zongid','=',$zong['id']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['code|buname|xiangname|unit|baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['code']) && !empty($param['code'])){
            $where[] = ['code','like', '%'.$param['code'].'%'];
        }
        if(isset($param['buname']) && !empty($param['buname'])){
            $where[] = ['buname','like', '%'.$param['buname'].'%'];
        }
        if(isset($param['xiangname']) && !empty($param['xiangname'])){
            $where[] = ['xiangname','like', '%'.$param['xiangname'].'%'];
        }
        if(isset($param['unit']) && !empty($param['unit'])){
            $where[] = ['unit','like', '%'.$param['unit'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['baner','like', '%'.$param['baner'].'%'];
        }
        $data = SpeedZongListModel::where($where)->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_speed_zong_list_page_speedmonth(Request $request){
        $param = $request->post();
        get_page($param);
        $zong = SpeedZongModel::where(['deleted_user'=>0,'enginid'=>$param['enginid']])->find();

        $where[] = ['zongid','=',$zong['id']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['code|buname|xiangname|unit|baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['code']) && !empty($param['code'])){
            $where[] = ['code','like', '%'.$param['code'].'%'];
        }
        if(isset($param['buname']) && !empty($param['buname'])){
            $where[] = ['buname','like', '%'.$param['buname'].'%'];
        }
        if(isset($param['xiangname']) && !empty($param['xiangname'])){
            $where[] = ['xiangname','like', '%'.$param['xiangname'].'%'];
        }
        if(isset($param['unit']) && !empty($param['unit'])){
            $where[] = ['unit','like', '%'.$param['unit'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['baner','like', '%'.$param['baner'].'%'];
        }
        $data = SpeedZongListModel::where($where)->select()->toArray();

        foreach ($data as &$item){
            $item['shifindt'] = EnginSpeedListModel::alias('list')
                ->join('engin_speed speed','speed.id=list.speedid')
                ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                ->where(['speed.enginid'=>$param['enginid'],'list.zongid'=>$item['id']])
                ->value('findt');

            $item['leishinum'] = EnginSpeedListModel::alias('list')
                ->join('engin_speed speed','speed.id=list.speedid')
                ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                ->where(['speed.enginid'=>$param['enginid'],'list.zongid'=>$item['id']])
                ->sum('shinum');

            $item['leishichan'] = round($item['leishinum']*$item['price'], 2);
            $item['leishiday'] = EnginSpeedListModel::alias('list')
                ->join('engin_speed speed','speed.id=list.speedid')
                ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                ->where(['speed.enginid'=>$param['enginid'],'list.zongid'=>$item['id']])
                ->sum('shiday');

            $item['dangjin'] = 0;
            if($item['num'] != 0){
                $item['dangjin'] = round($item['leishinum']/$item['num']*100, 2);
            }

            $item['gongbi'] = 0;
            if($item['duration'] != 0){
                $item['gongbi'] = round($item['leishiday']/$item['duration']*100, 2);
            }

            $item['gongjing'] = false;
            if($item['dangjin'] >= $item['gongbi']){
                $item['gongjing'] = true;
            }

            $item['dainum'] = $item['num']-$item['leishinum'];
            $item['daichan'] = $item['chanfee']-$item['leishichan'];
            $item['sheng'] = $item['duration']-$item['leishiday'];

            $item['zuijin'] = EnginSpeedListModel::alias('list')
                ->join('engin_speed speed','speed.id=list.speedid')
                ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                ->where(['speed.enginid'=>$param['enginid'],'list.zongid'=>$item['id']])
                ->order('speed.month DESC')
                ->value('speed.month');

        }

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_speed_month(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['month.deleted_user','=', 0];
        $where[] = ['month.comid','=',$this->userinfo['comid']];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['month.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|month.number|month.name|month.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['month.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['month.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $where[] = ['month.month','=', $param['month']];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['month.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['month.dt','between', explode(' - ', $param['dt'])];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }


        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = SpeedMonthModel::alias('month')
            ->field('month.*,e.name as enginname')
            ->join('engin e','e.id=month.enginid')
            ->where($where)
            ->order('month.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){


                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_speed_month_list(Request $request){
        $param = $request->post();
        get_page($param);
        $info = SpeedMonthModel::where(['deleted_user'=>0,'id'=>$param['monthid']])->find();
        $where[] = ['monthid','=',$info['id']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['code|buname|xiangname|unit','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['code']) && !empty($param['code'])){
            $where[] = ['code','like', '%'.$param['code'].'%'];
        }
        if(isset($param['buname']) && !empty($param['buname'])){
            $where[] = ['buname','like', '%'.$param['buname'].'%'];
        }
        if(isset($param['xiangname']) && !empty($param['xiangname'])){
            $where[] = ['xiangname','like', '%'.$param['xiangname'].'%'];
        }
        if(isset($param['unit']) && !empty($param['unit'])){
            $where[] = ['unit','like', '%'.$param['unit'].'%'];
        }

        $data = SpeedMonthListModel::where($where)->select()->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_labour_gong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ling.name|ling.idcard|ling.tel','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['ling.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['idcard']) && !empty($param['idcard'])){
            $where[] = ['ling.idcard','like', '%'.$param['idcard'].'%'];
        }
        if(isset($param['tel']) && !empty($param['tel'])){
            $where[] = ['ling.tel','like', '%'.$param['tel'].'%'];
        }
        if(isset($param['yongxing']) && !empty($param['yongxing'])){
            $where[] = ['ling.yongxing','=', $param['yongxing']];
        }
        if(isset($param['gongtype']) && !empty($param['gongtype'])){
            $where[] = ['ling.gongtype','=', $param['gongtype']];
        }

        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['ling.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['ling.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['ling.banknum','like', '%'.$param['banknum'].'%'];
        }

        $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
        $data['is_yongxing'] = isset($param['yongxing']) ? $param['yongxing'] : '';

        $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
        $data['is_gongtype'] = isset($param['gongtype']) ? $param['gongtype'] : '';



        $data['data'] = LabourGongModel::alias('ling')
            ->field('ling.*')
            ->where($where)
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['gongtype_name'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_labour_xiang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['xiang.deleted_user','=', 0];
        $where[] = ['xiang.comid','=',$this->userinfo['comid']];
        $where[] = ['xiang.isturn','=',1];
        $where[] = ['xiang.status','=', 1];
        $where[] = ['xiang.enginid','=', $param['enginid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['list.name|list.idcard','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listidcard']) && !empty($param['listidcard'])){
            $where[] = ['list.idcard','like', '%'.$param['listidcard'].'%'];
        }
        if(isset($param['listtel']) && !empty($param['listtel'])){
            $where[] = ['list.tel','like', '%'.$param['listtel'].'%'];
        }
        if(isset($param['yongxing']) && !empty($param['yongxing'])){
            $where[] = ['list.yongxing','=', $param['yongxing']];
        }
        if(isset($param['gongtype']) && !empty($param['gongtype'])){
            $where[] = ['list.gongtype','=', $param['gongtype']];
        }
        if(isset($param['gonglei']) && !empty($param['gonglei'])){
            $where[] = ['list.gonglei','=', $param['gonglei']];
        }

         $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
        $data['is_yongxing'] = isset($param['yongxing']) ? $param['yongxing'] : '';

        $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
        $data['is_gongtype'] = isset($param['gongtype']) ? $param['gongtype'] : '';

        $data['gonglei'] = (new OptService())->get_base_info('engin_labour_gonglei');
        $data['is_gonglei'] = isset($param['gonglei']) ? $param['gonglei'] : '';



        $data['data'] = LabourXiangListModel::alias('list')
            ->field('list.*,e.name as enginname')
            ->join('labour_xiang xiang','xiang.id=list.xiangid')
            ->join('engin e','e.id=xiang.enginid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['gongtype_name'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);
                $item['gonglei_name'] = (new OptService())->get_base_showname('engin_labour_gonglei', $item['gonglei']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_device_jin_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','=', 1];
        $where[] = ['jin.enginid','=', $param['enginid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['list.name|list.shenum|cai.name|cai.number|cai.gonger|jin.number','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['caigonger']) && !empty($param['caigonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['caigonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jin.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jin.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jin.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listunit']) && !empty($param['listunit'])){
            $where[] = ['list.unit','like', '%'.$param['listunit'].'%'];
        }
        if(isset($param['listprice']) && !empty($param['listprice'])){
            $where[] = ['list.price','like', '%'.$param['listprice'].'%'];
        }
        if(isset($param['listdigit']) && !empty($param['listdigit'])){
            $where[] = ['list.digit','like', '%'.$param['listdigit'].'%'];
        }
        if(isset($param['listjindt']) && !empty($param['listjindt'])){
            $where[] = ['list.jindt','between', explode(' - ', $param['listjindt'])];
        }
        if(isset($param['listshenum']) && !empty($param['listshenum'])){
            $where[] = ['list.shenum','like', '%'.$param['listshenum'].'%'];
        }

        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['list.state','like', '%'.$param['state'].'%'];
        }

        $data['data'] = DeviceJinListModel::alias('list')
            ->field('jin.*,e.name as enginname,cai.name as cainame,cai.gonger
            ,list.id as listid,list.name as listname,list.unit as listunit,list.price as listprice
            ,list.digit as listdigit,list.jindt as listjindt,list.shenum as listshenum,list.state
            ,list.yununit,list.tuidt')
            ->join('device_jin jin','jin.id=list.jinid')
            ->join('engin e','e.id=jin.enginid')
            ->join('contract_cai cai','cai.id=jin.caiid')
            ->where($where)
            ->order('jin.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['state'] == '退场'){
                    $item['yundays'] = count_days($item['listjindt'], $item['tuidt']);
                }else{
                    $item['yundays'] = count_days($item['listjindt'], date("Y-m-d"));
                }
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_device_jin_list_page_devicetui(Request $request){
        $param = $request->post();

        $where[] = ['jin.deleted_user','=', 0];
        $where[] = ['jin.comid','=',$this->userinfo['comid']];
        $where[] = ['jin.isturn','=',1];
        $where[] = ['jin.status','=', 1];
        $where[] = ['jin.caiid','=', $param['caiid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|cai.name|cai.number|cai.gonger|jin.number|list.name|list.unit|list.price|list.digit|list.shenum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['caigonger']) && !empty($param['caigonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['caigonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['jin.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['jin.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['jin.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['listunit']) && !empty($param['listunit'])){
            $where[] = ['list.unit','like', '%'.$param['listunit'].'%'];
        }
        if(isset($param['listprice']) && !empty($param['listprice'])){
            $where[] = ['list.price','like', '%'.$param['listprice'].'%'];
        }
        if(isset($param['listdigit']) && !empty($param['listdigit'])){
            $where[] = ['list.digit','like', '%'.$param['listdigit'].'%'];
        }
        if(isset($param['listjindt']) && !empty($param['listjindt'])){
            $where[] = ['list.jindt','between', explode(' - ', $param['listjindt'])];
        }
        if(isset($param['listshenum']) && !empty($param['listshenum'])){
            $where[] = ['list.shenum','like', '%'.$param['listshenum'].'%'];
        }


        $info = [];
        $list = DeviceJinListModel::alias('list')
            ->field('jin.*,e.name as enginname,cai.name as cainame,cai.gonger,list.id as listid,list.name as listname
            ,list.unit as listunit,list.price as listprice,list.digit as listdigit,list.jindt as listjindt,
            list.shenum as listshenum')
            ->join('device_jin jin','jin.id=list.jinid')
            ->join('engin e','e.id=jin.enginid')
            ->join('contract_cai cai','cai.id=jin.caiid')
            ->where($where)
            ->order('jin.id DESC')
            ->select()->toArray();

        foreach ($list as $item){
            $item['status_name'] = '在场';
            $tui = DeviceTuiModel::where(['enginid'=>$item['enginid'],'caiid'=>$item['caiid'],'deleted_user'=>0,'jinid'=>$item['listid']])->find();
            if(!$tui){
                $item['yununit'] = $yununit = DeviceYunModel::where(['enginid'=>$item['enginid'],'caiid'=>$item['caiid'],'deleted_user'=>0,'jinid'=>$item['listid']])->sum('yununit');
                $info[] = $item;
            }
        }



        $data['data'] = $info;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function get_dai_kou_qi_list(Request $request){
        $param = $request->post();
        get_page($param);
        $where[] = ['dai.deleted_user','=', 0];
        $where[] = ['dai.comid','=',$this->userinfo['comid']];
        $where[] = ['dai.isturn','=',1];
        $where[] = ['dai.status','=',1];
        $where[] = ['list.kouid','<', 1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['dai.enginid','=', $param['enginid']];
        }


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|dai.zhai|dai.flowuser','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['dai.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['flowuser']) && !empty($param['flowuser'])){
            $where[] = ['dai.flowuser','like', '%'.$param['flowuser'].'%'];
        }
        if(isset($param['created_at']) && !empty($param['created_at'])){
            $created_atArr = explode(' - ', $param['created_at']);
            $where[] = ['dai.created_at','between', [$created_atArr[0].' 00:00:00', $created_atArr[1].' 23:59:59']];
        }
        if(isset($param['feitype']) && !empty($param['feitype'])){
            $where[] = ['list.feitype','=', $param['feitype']];
        }

        $data['feitype'] = (new UserService())->get_option_fei_slet(['xmsr_lyfyhks']);
        $data['is_feitype'] = isset($param['feitype']) ? $param['feitype'] : '';

        $data['data'] = DaiKouQiListModel::alias('list')
            ->field('dai.*,e.name as enginname,bill.sericnum,list.id as listid,list.kouid,
            list.feitype,list.money as listmoney,list.explain as listexplain')
            ->join('dai_kou_qi dai','dai.id=list.kouqiid')
            ->join('engin e','e.id=dai.enginid')
            ->leftJoin('flow_bill bill','bill.id=dai.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('dai_kou_qi', 'dai'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);

                $item['state_name'] = $item['kouid'] > 0 ? '已扣收' : '待扣收';
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }

}





















